<template>
  <div class="Home">
    <div class="login-nav">
      <div class="login-nav-logo">
        <img src="../assets/icon/loginlogo.png" alt="" width="150px" />
      </div>
      <div class="login-nav-gg" @click="HomeView">逛一逛</div>
    </div>

    <div class="welcometext">
      <div class="chinese">找回密码！</div>
      <div class="english">Find password</div>
    </div>
    <div class="loginInfo">
      <div class="code">
        <van-button  type="info" :disabled="isClick ? true : false" @click="getEmail">{{isClick ? time : "发送验证码"}}</van-button>
      </div>
      <van-form @submit="getCode">
        <van-field
          v-model="userEmail"
          name="邮箱"
          label="邮箱"
          placeholder="邮箱"
          autocomplete="off"
        />
        <div class="codebox">
          <div class="yzm">
            <van-field
              v-model="code"
              name="验证码"
              label="验证码"
              autocomplete="off"
              placeholder="验证码(6个字符)"
            />
          </div>
        </div>
        <div class="forgotpas" @click="Tologin">已有帐号,立即登录</div>
        <div class="btn">
          <van-button round block type="info" native-type="submit"
            >下一步</van-button
          >
        </div>
      </van-form>
      
    </div>
  </div>
</template>

<script>
export default {
  name: "Email",
  data() {
    return {
      time: 59,
      isClick: false,
      code: "",
      userEmail: "",
      appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
    };
  },
  created() {},
  methods: {
    //跳转登录
    Tologin() {
      this.$router.push({ name: "Login" });
    },
    //下一步
    ToNewpassword() {
      this.$router.push({ name: "Newpassword" });
    },
    HomeView() {
      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
      });
      setTimeout(() => {
        this.$router.push({ name: "Home" });
        this.$toast.clear();
      }, 3000);
    },
    getEmail() {
      let str = `appkey=${this.appkey}&email=${this.userEmail}`;
      this.axios({
        method: "post",
        url: "/emailValidCode",
        data: str,
      }).then((res) => {
        this.isClick = !this.isClick;
        console.log(res);
        let timer = null;
        timer = setInterval(() => {
          if (this.time <= 1) {
            clearInterval(timer);
            this.isClick = !this.isClick;
            this.time = 59;
            return;
          }
          this.time--;
        }, 1000);
      });
    },
    //验证验证码
    getCode() {
      console.log(123);
      //验证邮箱
      let RegEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
      if (!RegEmail.test(this.userEmail) || this.userEmail == "") {
        this.$toast("邮箱格式错误");
        return;
      }
      if (this.code == "") {
        this.$toast("验证码为空");
        return;
      }
      let str = `appkey=${this.appkey}&validCode=${this.code}`;
      this.axios({
        method: "post",
        url: "/checkValidCode",
        data: str,
      }).then((res) => {
        this.$router.push({ name: "Newpassword" });
        console.log(res);
      });
    },
  },
};
</script>

<style lang="less" scoped>
.btn {
  /deep/.van-button--round{
    width: 350px;
    margin-left: 3px;
  }
  margin: 60px 10px;
}
.code {
  /deep/.van-button--normal{
    height: 38px;
  }
  margin-top: 44px;
  position:absolute;
  z-index: 1;
  // width: 100px;
  margin-left: 273px;
}
.forgotpas {
  color: #6a6867;
  font-size: 12px;
  width: 200px;
  margin-top: 15px;
  margin-left: 265px;
}
.loginInfo {
  position:fixed;
  margin-top: 40px;
}
.english {
  margin-left: 10px;
  font-size: 14px;
  margin-top: 20px;
  color: #83838b;
}
.chinese {
  margin-left: 10px;
  margin-top: 80px;
  font-weight: bold;
  font-size: 24px;
  color: #646566;
}
.login-nav-gg {
  margin-top: 3px;
  color: blue;
}
.login-nav {
  display: flex;
  justify-content: space-between;
  padding: 10px;
  box-sizing: border-box;
  align-items: center;
  height: 50px;
  font-size: 16px;
}
.Home {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #fff;
}
</style>